package uiLayer;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.JButton;
import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;

import javax.swing.border.TitledBorder;

import modelLayer.Customer;
import java.awt.FlowLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.border.BevelBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import java.util.Date;

import controlLayer.CustomerCtr;
import controlLayer.OrderCtr;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SaleTab2 extends JPanel {
	
	private JTextField txtDate;
	private JTextField txtCustomerID;
	private JTextField txtCustomerName;	
	private CustomerCtr customerCtr;
	private OrderCtr orderCtr;
	private Customer customer;
	private String customerName;
	private JTextField txtName;
	private JTextField txtAddress;
	private JTextField txtZipCode;
	private JTextField txtCity;
	private JTextField txtPhone;
	
	public SaleTab2(OrderCtr orderCtr) {
		
		this.setOrderCtr(orderCtr);
		
		this.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentResized(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentMoved(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void componentHidden(ComponentEvent arg0) {
				saveTab2();
				
			}
		});
		
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel topField = new JPanel();
		topField.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(topField, BorderLayout.NORTH);
		topField.setLayout(new BorderLayout(0, 0));
		
		JPanel dateField = new JPanel();
		dateField.setBorder(new EmptyBorder(5, 5, 5, 5));
		topField.add(dateField, BorderLayout.EAST);
		dateField.setLayout(new BorderLayout(0, 0));
		
		this.txtDate = new JTextField();
		this.txtDate.setText("" + new Date());
		this.txtDate.setEditable(false);
		dateField.add(this.txtDate, BorderLayout.NORTH);
		this.txtDate.setColumns(10);
		
		JPanel left = new JPanel();
		left.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null), "Find kunde", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		topField.add(left, BorderLayout.CENTER);
		left.setLayout(new BorderLayout(0, 0));
		
		JPanel searchPanel = new JPanel();
		left.add(searchPanel);
		searchPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		searchPanel.setLayout(new GridLayout(3, 2, 5, 5));
		
		JLabel lblCustomerID = new JLabel("KundeID:");
		searchPanel.add(lblCustomerID);
		
		this.txtCustomerID = new JTextField();
		this.txtCustomerID.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent e) {
				
				adjustSearchFields();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				
				adjustSearchFields();
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				
				adjustSearchFields();
			}
				
		}); 
			
		
		searchPanel.add(this.txtCustomerID);
		this.txtCustomerID.setColumns(10);
		
		JLabel lblCustomerName = new JLabel("Kundenavn:");
		searchPanel.add(lblCustomerName);
		
		this.txtCustomerName = new JTextField();
		this.txtCustomerName.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent e) {
				
				adjustSearchFields();
				
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				adjustSearchFields();
				
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				adjustSearchFields();
				
			}
			
		});
		searchPanel.add(this.txtCustomerName);
		this.txtCustomerName.setColumns(10);
		
		JPanel panelButton = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panelButton.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		left.add(panelButton, BorderLayout.SOUTH);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				findCustomerByName();
				findCustomerByID();
				handleClickedOk();
			}
		});
		panelButton.add(btnOk);
		
		JPanel buttonPanel = new JPanel();
		panel.add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JButton btnAnnuller = new JButton("Annuller");
		btnAnnuller.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				handleClickedCancel();
			}
		});
		buttonPanel.add(btnAnnuller);
		
		JButton btnNste = new JButton("Næste");
		btnNste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				handleClickedNext();
			}
		});
		buttonPanel.add(btnNste);
		
		JPanel centerPanel = new JPanel();
		panel.add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel wrapPanel = new JPanel();
		wrapPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		centerPanel.add(wrapPanel, BorderLayout.WEST);
		
		JPanel customerPanel = new JPanel();
		customerPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		wrapPanel.add(customerPanel);
		customerPanel.setLayout(new GridLayout(5, 2, 5, 5));
		
		JLabel lblNavn = new JLabel("Navn:");
		customerPanel.add(lblNavn);
		
		this.txtName = new JTextField();
		customerPanel.add(this.txtName);
		this.txtName.setColumns(10);
		
		JLabel lblAddress = new JLabel("Adresse:");
		customerPanel.add(lblAddress);
		
		this.txtAddress = new JTextField();
		customerPanel.add(this.txtAddress);
		this.txtAddress.setColumns(10);
		
		JLabel lblPostnummer = new JLabel("Postnummer:");
		customerPanel.add(lblPostnummer);
		
		this.txtZipCode = new JTextField();
		customerPanel.add(this.txtZipCode);
		this.txtZipCode.setColumns(10);
		
		JLabel lblBy = new JLabel("By:");
		customerPanel.add(lblBy);
		
		this.txtCity = new JTextField();
		customerPanel.add(this.txtCity);
		this.txtCity.setColumns(10);
		
		JLabel lblTlf = new JLabel("Tlf.");
		customerPanel.add(lblTlf);
		
		this.txtPhone = new JTextField();
		customerPanel.add(this.txtPhone);
		this.txtPhone.setColumns(10);
		
		
		
		
	}
	
	public void clearFields() {
		
		this.txtName.setText("");
		this.txtCustomerID.setText("");
		this.txtCustomerName.setText("");
		this.txtAddress.setText("");
		this.txtZipCode.setText("");
		this.txtCity.setText("");
		this.txtPhone.setText("");
		
	}
	
	public void findCustomerByName() {
		
		if(!this.txtCustomerName.getText().isEmpty()) {
			this.customerCtr = new CustomerCtr();
		
		String name = txtCustomerName.getText();
		
		this.customer = this.customerCtr.findCustomerName(name);
		putValueOnScreen(this.customer);
		};
		
	}
	
	public void findCustomerByID() {
		
		if(!this.txtCustomerID.getText().isEmpty()) {
		
			this.customerCtr = new CustomerCtr();
		
		
		int ID = Integer.parseInt(this.txtCustomerID.getText());
		
		this.customer = this.customerCtr.findCustomerID(ID);
		putValueOnScreen(this.customer);	
		}
	
		
	}
	
	public void putValueOnScreen(Customer customer) {
		
		if(customer != null) {
		this.txtName.setText(customer.getName());
		this.txtAddress.setText(customer.getAddress());
		this.txtZipCode.setText(String.valueOf(customer.getZipCode()));
		this.txtCity.setText(customer.getCity());
		this.txtPhone.setText(customer.getPhoneNo()); }
		else {
			JOptionPane.showMessageDialog(null, "Der er ingen kunde tilknyttet det ID eller navn ");
		}
		
			
	}
	
	private void handleClickedNext() {
		
		JTabbedPane jtp = (JTabbedPane)this.getParent();
		jtp.setSelectedIndex(jtp.getSelectedIndex() + 1);		
		
	}
	
	private void handleClickedOk() {
				
		if(!this.txtName.getText().isEmpty()) {			
			this.customerName = this.txtName.getText();		
		}
		else if(this.txtCustomerName.getText().isEmpty() && this.txtCustomerID.getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Du mangler at tilf�je en kunde");
		}
			
	}
	
	private void handleClickedCancel() {
		
		if(this.orderCtr.getSalesOrder().getCustomer() != null) {
			this.orderCtr.getSalesOrder().setCustomer(null);
		}
		clearFields();
		
	}
	
	protected void adjustSearchFields() {
		
		if(!this.txtCustomerID.getText().isEmpty()) {		
			this.txtCustomerName.setEditable(false);			
		}
		else if(!this.txtCustomerName.getText().isEmpty()) {			
			this.txtCustomerID.setEditable(false);			
		}
		else {			
			this.txtCustomerID.setEditable(true);
			this.txtCustomerName.setEditable(true);			
		}
		
	}
		

	public OrderCtr getOrderCtr() {
		
		return this.orderCtr;
		
	}

	private void setOrderCtr(OrderCtr orderCtr) {
		
		this.orderCtr = orderCtr;
		
	}
	

	public void saveTab2() {
		
		orderCtr.addCustomer(this.customerName);
		clearFields();
	
	}
	
}
